package com.dingdang.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import com.dingdang.dto.FenQiDTO;
import com.dingdang.service.SendFenQiMessagesServiceI;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class SendFenQiMessagesServiceImpl implements SendFenQiMessagesServiceI
{
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public void sendMessage(FenQiDTO fenQiDTO)
    {
        Client client = null;
        try {
            System.out.println("您该还款了！总金额是：" + fenQiDTO.getFenqiAmmont() + "还有:" + Integer.valueOf(fenQiDTO.getFenqiFenqishu()) + "期");
            client = createClient("LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
            String numeric = RandomStringUtils.randomNumeric(6);
            String fenqiFenqishu = fenQiDTO.getFenqiFenqishu();
            BigDecimal fenqiAmmont = fenQiDTO.getFenqiAmmont();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(fenQiDTO.getFenqiPhone())//手机号
                    //标题
                    .setSignName("叮当二手车")
                    .setTemplateCode("SMS_162522027")
                    .setTemplateParam("{\"sms_code\":\""+ numeric + "\"}");//信息内容
            // 复制代码运行请自行打印 API 的返回值
            client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public Client createClient (String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }


}
